import VueRouter from "vue-router";
import Home from "../pages/Home.vue";
import About from "../pages/About.vue";
import News from "../pages/News.vue";
import Message from "../pages/Message.vue";
import Detail from "../pages/Detail.vue";
const router = new VueRouter({
    routes:[
        {
            path:"/home",
            component:Home,
            meta:{
                //添加是否需要授权进入的字段
                isAuth: true,
                 title: "主页"
            },
            children:[
                {
                    meta:{
                        isAuth: true,
                        title: "新闻"
                    },
                    path:'news',
                    component: News,
                    children:[
                        {   
                            name:'xiangqing',
                            path:'detail/:id/:title',
                            component: Detail,
                            props: true,
                        }
                    ]
                },
                {
                    path:'message',
                    component: Message,
                    children:[
                        {   
                            name:'xiangqing',
                            path:'detail/:id/:title',
                            component: Detail,
                            //props对象写法(很少用)
                            //props:{a:'1',b:'2'}
                            //props布尔写法,
                            props: true,
                        }
                    ]

                }
            ],
        },
        {
            path:"/about",
            component:About
        }
    ]
});
//全局前置路由守卫
router.beforeEach((to, from, next)=>{

    console.log(localStorage.getItem("auth"))

    if(to.meta.isAuth){
        if(localStorage.getItem("auth") === "1234") next()
    }else{
        next()
    }
    console.log("to-----",to)
    console.log("from-----",from)
    console.log("next-----",next)
})


//全局后置路由首位
router.afterEach((to, from)=>{

     document.title = to.meta.title ?? "to.meta.title"

})

export default router;